const validator=require('validator');
module.exports=function (done) {
	$.method('user.add').check({
		name: {required: true,validate: v => validator.isLength(v,{min:4,max:10})&&/^[a-zA-Z]/.test(v)},
		email: {required: true,validator:v => validator.isEmail(v)},
		password: {
			required: true,validator: v=>validator.isLength(v,{min:6})}
	});
	$.method('user.add').register(async function (params) {
		params.name=params.name.toLowerCase();
		{
			const user=await $.method('user.get').call({name: params.name});
			if (user) throw new Error(`user ${params.name} already exists`);
		}
		{
			const user=await $.method('user.get').call({email: params.email});
			if (user) throw new Error(`user ${parmas.email} already exists`);
		}
		params.password=$.utils.encryptPassword(params.password.toString());
		const user=new $.model.User({
			name: params.name,
			email: params.email,
			nickname: params.nickname,
			password: params.password,
			about:params.about
		});
		return user.save();
	});
	$.method('user.get').check({
		_id: {validate: v => validator.isMongoId(String(v))},
		name: {validato: v => validator.isLength(v,{min: 4,max: 20})&&/^[a-zA-z]/.test(v)},
		email:{validate:v=>validator.isEmail(v)}
	});
	$.method('user.get').register(async function (params) {
		return $.model.User.findOne(params);
	});
	done();
}